//
//  UUCarEditVC.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 25/01/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUCarEditVC.h"
#import <UUFoundation/DKProgressHUD.h>
#import "UUHybridManager+CharterSupplier.h"
#import "UUCarManager.h"
#import "UUFleetManager.h"
#import <UUFoundation/UUFileManager.h>
#import "UUPermissionManager.h"

@interface UUCarEditVC () <UUCarManagerObserver>

@end

@implementation UUCarEditVC

- (void)viewDidLoad {
    if (!self.editable) {
        [GetCarManager() addObserver:self];
    }

    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSString *)name {
    return @"车辆";
}

- (BOOL)isAdd {
    return self.carId == nil;
}

- (BOOL)canEdit {
    return [GetPermissionManager() checkPermission:UUPermissionFlagCarReadWrite];
}

- (void)configEditVC:(UUCarEditVC *)editVC {
    editVC.carId = self.carId;
}

- (void)enterEditVC {
    UUCarModel *car = [GetCarManager() fetchCarWithCarId:self.carId];
    if (car.status == UUCarReviewStatusApproval) {
        [UIAlertController presentAlertControllerWithTitle:@"重新编辑提交后该车辆需要进行重新审核" message:nil doneBlock:^{
            [super enterEditVC];
        } cancelBlock:nil];
    } else {
        [super enterEditVC];
    }
}

- (NSURLRequest *)editRequest {
    return GetHybridManager().carEditURLRequest;
}

- (NSURLRequest *)detailRequest {
    return GetHybridManager().carDetailURLRequest;
}

- (void)addOrUpdate:(NSDictionary *)data {
    NSNumber *fleetId = data[@"motorcade_id"];
    NSString *carNum = data[@"car_num"];
    NSNumber *carType = data[@"car_type"];
    NSString *vehicleLicense = data[@"vehicle_license"];
    NSString *vehicleLicenseDeputy = data[@"vehicle_license_deputy"];
    NSString *insurance = data[@"insurance"];
    NSString *transportLicense = data[@"transport_license"];
    BOOL iSself = [data[@"is_self"] boolValue];
    
    WEAK_SELF();
    void (^completeBlock)(NSError *error) = ^(NSError *error) {
        HideIndicatorInView(weakSelf.view);
        
        if (error == nil) {
            BLOCK_SAFE_CALLS(weakSelf.carDidAddBlock);
            
            NSString *tips = [NSString stringWithFormat:@"%@成功", weakSelf.carId == nil ? @"添加" : @"更新"];
            ShowTipsInKeyWindow(tips);
            [weakSelf.navigationController popViewControllerAnimated:YES];
        } else {
            ShowTipsOnView(error.localizedDescription, weakSelf.view);
        }
    };
    
    ShowIndicator();
    
    if (self.carId == nil) {
        [GetCarManager() addCarWithFleetId:fleetId
                                    carNum:carNum
                                   carType:carType
                                    isSelf:iSself
                            vehicleLicense:vehicleLicense
                      vehicleLicenseDeputy:vehicleLicenseDeputy
                                 insurance:insurance
                          transportLicense:transportLicense
                             completeBlock:completeBlock];
    } else {
        [GetCarManager() updateCarWithCarId:self.carId
                                    fleetId:fleetId
                                     carNum:carNum
                                    carType:carType
                                     isSelf:iSself
                             vehicleLicense:vehicleLicense
                       vehicleLicenseDeputy:vehicleLicenseDeputy
                                  insurance:insurance
                           transportLicense:transportLicense
                              completeBlock:completeBlock];
    }
}

- (NSDictionary *)previewFields {
    if (self.carId != nil) {
        UUCarModel *car = [GetCarManager() fetchCarWithCarId:self.carId];
        UUCarCategoryModel *carCategory = [GetCarCategoryManager() fetchCarCategoryWithCategoryId:car.carType];
        UUFleetModel *fleet = [GetFleetManager() fetchFleetWithFleetId:car.fleetId];
        
        if (car && fleet && carCategory) {
            return @{
                     @"id" : self.carId,
                     @"motorcade_id" : car.fleetId,
                     @"motorcade_name" : fleet.name,
                     @"car_num" : car.carNum,
                     @"is_self" : @(car.isSelf),
                     @"car_type" : car.carType,
                     @"car_type_name" : carCategory.name,
                     @"insurance" : [GetFileManager() generatePreferredPreviewImageURLWithKey:car.insurance],
                     @"vehicle_license" : [GetFileManager() generatePreferredPreviewImageURLWithKey:car.vehicleLicense],
                     @"vehicle_license_deputy" : [GetFileManager() generatePreferredPreviewImageURLWithKey:car.vehicleLicenseDeputy],
                     @"transport_license" : [GetFileManager() generatePreferredPreviewImageURLWithKey:car.transportLicense],
                     @"status" : @(car.status),
                     @"remark" : car.remark ?: @"",
                     };
        } else {
            return nil;
        }
    } else {
        return nil;
    }
}

#pragma mark - UUCarManagerObserver methods

- (void)carManagerDidUpdateCars {
    if (self.carId != nil) {
        [self reloadForm];
    }
}

@end
